//
//  FeedbackViewController.m
//  图片故事
//
//  Created by 熊坤 on 16/3/2.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "FeedbackViewController.h"
#import "Header.h"
#import <BmobSDK/Bmob.h>
#import <BmobSDK/BmobQuery.h>
#import "UserInfoModal.h"
#import "LocaData.h"
#import "MBProgressHUD+PKX.h"

@interface FeedbackViewController () <UITextViewDelegate>

@property (nonatomic, strong) UITextView *textVIew;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UIButton *submitBtn;

@end

@implementation FeedbackViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:Bg_Color];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self initNavigation];
    [self loadWidgetView];
   
    
}

- (void)loadWidgetView {
    
    [self.view addSubview:self.textVIew];
    [self.view addSubview:self.numLabel];
    [self.view addSubview:self.submitBtn];
}

- (void)initNavigation {
    
    self.title = @"意见反馈";
    
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
}

#pragma mark -- 点击事件
- (void)action_backItmeClickEvent {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)submitBtnClicked {
        
        NSString *userName = nil;
        if ([LocaData isLogin] == YES) {
            
            UserInfoModal *userInfo = [LocaData readUserInfo];
            userName = userInfo.userName;

        }
        BmobObject *object = [BmobObject objectWithClassName:@"feedBack"];
    
    
        [object setObject:userName forKey:@"userName"];
        
        [object setObject:self.textVIew.text forKey:@"feedBackMsg"];
        
        [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                [MBProgressHUD showSuccess:@"提交成功，感谢您的反馈！"];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [MBProgressHUD showError:@"提交失败"];
            }
            
        }];
    
    
    
}

#pragma mark -- 懒加载
- (UITextView *)textVIew {
    if (!_textVIew) {
        
        _textVIew = [[UITextView alloc] init];
        _textVIew.center = CGPointMake(SCREEN_W / 2, My_Height(120));
        _textVIew.bounds = CGRectMake(0, 0, My_Width(340), My_Height(200));
        _textVIew.backgroundColor = Btn_Color;
        _textVIew.textAlignment = NSTextAlignmentLeft;
        _textVIew.delegate = self;
        [_textVIew setTextColor:[UIColor grayColor]];
        _textVIew.text = @"请描述你要反馈的问题或意见";
        _textVIew.alwaysBounceVertical = NO;
    }
    return _textVIew;
}

- (UILabel *)numLabel {
    if (!_numLabel) {
        _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.textVIew.frame) - My_Width(40) - 10, CGRectGetMaxY(self.textVIew.frame) - My_Height(20) , My_Width(60), My_Height(20))];
        _numLabel.textColor = [UIColor grayColor];
        _numLabel.font = [UIFont systemFontOfSize:12];
        _numLabel.text = @"0 / 200";
    }
    return _numLabel;
}

- (UIButton *)submitBtn {
    if (!_submitBtn) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitBtn.center = CGPointMake(SCREEN_W / 2, CGRectGetMaxY(self.textVIew.frame) + My_Height(44));
        _submitBtn.bounds = CGRectMake(0, 0, My_Width(340), My_Height(44));
        _submitBtn.backgroundColor = [UIColor colorWithRed:102 / 255.0 green:214 / 255.0 blue:228 / 255.0 alpha:1];
        [_submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        _submitBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_submitBtn.layer setCornerRadius:5.0];
        [_submitBtn addTarget:self action:@selector(submitBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitBtn;
}

#pragma mark --  代理


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if (textView.tag == 0) {
        textView.text = @"";
        textView.tag = 1;
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length == 0) {
        _textVIew.tag = 0;
    }
    
    self.numLabel.text = [NSString stringWithFormat:@"%ld / 200",textView.text.length];
    
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (range.location >=200) {
        [MBProgressHUD showError:@"您已经输入了两百字，最大能输入两百字"];
        return NO;
    } else {
        return YES;
    }
}
@end
